<!-- 组织架构 -->

<template >
  <div class="container">
    <div class="app-container" v-loading="isLoading">
      <el-tree default-expand-all :expand-on-click-node="false" :data="treeData" :props="defaultProps">
        <!-- 节点结构 -->
        <template v-slot="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <!-- 部门 -->
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4" class="temp-el-col">
              <!-- 部门负责人 -->
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- command,当前点击部门id -->
              <el-dropdown @command="handleCommand($event, data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 弹窗复用组件 -->
    <bullet-box @updateDepartment="getDepartment" :parentId="parentId" :isEdit="isEdit" :showDialog.sync="showDialog"
      ref="bulletBoxRef"></bullet-box>
  </div>
</template>

<script>
import { getDepartmentAPI, delDepartmentAPI } from "@/api/department.js"
import { listToTreeData } from "@/utils/index.js"
import bulletBox from "./components/bullet-box.vue"

export default {
  name: 'Department',
  data() {
    return {
      isLoading: true,
      // 树形数据
      treeData: [],
      // 数据展示字段名
      defaultProps: {
        label: 'name', // 节点
        children: 'children'  // 子节点
      },
      showDialog: false, // 控制弹窗显示隐藏
      isEdit: false,  // 是否编辑状态
      parentId: null // 父级部门id
    }
  },
  components: { bulletBox },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取部门列表
    async getDepartment() {
      this.isLoading = true
      const res = await getDepartmentAPI()
      this.treeData = listToTreeData(res, 0)
      this.isLoading = false
    },
    // 下拉框点击事件
    handleCommand(command, parentId) {
      if (command === 'del') {  // 删除
        this.$confirm('是否删除此部门?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await delDepartmentAPI(parentId)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getDepartment()  // 更新部门列表
        })
      } else {  // 新增和编辑
        this.isEdit = (command === 'edit' ? true : false) // 是否编辑状态
        this.parentId = parentId  // 父部门id
        this.showDialog = true  // 显示弹窗
        this.$nextTick(() => {
          // 是编辑状态才调用
          this.isEdit && this.$refs.bulletBoxRef.getDepartmentDetail() // 调用子组件方法
        })
      }
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}

.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}

.temp-el-col {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>

